package Klient.Network;

import Klient.AbstractKlientCommunicator;
import Klient.Message;
import Server.ServerCommunicator;
import java.io.*;
import java.net.Socket;

/**
 *
 * @author Martin Strouhal
 */
public class KlientCommunicator implements AbstractKlientCommunicator {

    private String welcomeMessage = "Hello, I am Totne";
    private OutputStream outputStream;
    private InputStream inputStream;
    private ObjectInputStream objectInputStream;
    private Thread prijemZprav = new Thread(new Runnable() {

        @Override
        public void run() {
            while (true) {
                try {
                    process((Message) objectInputStream.readObject());
                } catch (IOException ex) {
                    System.out.println(ex.getMessage());
                } catch (ClassNotFoundException ex) {
                    System.out.println(ex.getMessage());
                }
            }
        }
    });
    
    private void process(Message m){
        throw new Error("Not suprted yet");
    }

    @Override
    public void sendMessage(Message m) {
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(outputStream);
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }
        try {
            oos.writeObject(m);
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }
        try {
            oos.flush();
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }
        try {
            oos.close();
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }
    }

    @Override
    public void verify(Socket socket) throws ConnectionException {
        try {
            outputStream = socket.getOutputStream();
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }
        PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputStream));
        writer.write(welcomeMessage);
        writer.flush();

        try {
            inputStream = socket.getInputStream();
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        String readLine = null;
        try {
            readLine = reader.readLine();
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }
        if (readLine.compareTo(ServerCommunicator.klientAdded) == 0) {
            try {
                objectInputStream = new ObjectInputStream(inputStream);
            } catch (IOException ex) {
                System.out.println(ex.getMessage());
            }
        } else {
            inputStream = null;
            outputStream = null;
            throw new ConnectionException();
        }
    }

    @Override
    public void chceckingMessages() {
        prijemZprav.start();
    }
}